var windowName = "userCertificate";
var Page,count = 0,UserPics,HeadPics;
apiready = function(){
    var imageBrowser = api.require('imageBrowser')
    UserPics = new PicUp()
    onWindowShow(function () {
        console.log(windowName + " show")
    })
    onWindowHide(function () {
        console.log(windowName + " hide")
    })
    console.log("index page is OK");
    Page = createVM("#Page",{
        name:""
        ,pics:[]
        ,picsView:[]
        ,log:[]
    },{
        //页面方法
        "init":function () {
            getData()
            var _this = this;
            UserPics.setCbFunc(function (data) {
                logJson(data,"pics")
                _this.pics = UserPics.getData()
                _this.picsView = UserPics.getViewData()
                logJson(_this.picsView,"picsView")

            })
        },
        selectPic:function () {
            UserPics.chooseSrc(9)
            // UserPics.select(9).setCbFunc(function (data) {
            //     logJson(data,"pics")
            //     _this.pics = UserPics.getData()
            //     _this.picsView = UserPics.getViewData()
            //     logJson(_this.picsView,"picsView")
            //
            // })
        },
        del:function (index) {
            var pics = UserPics.removePic(index)
            this.picsView = UserPics.getViewData()
            this.pics = UserPics.getData()
        },
        move:function (index,num) {
            var pics = UserPics.picMove(index,num)
            this.picsView = UserPics.getViewData()
            this.pics = UserPics.getData()
        },
        showPic:function (index,pics) {
            imageBrowser.openImages({
                imageUrls: pics,
                activeIndex:index
            });
        }
        ,submit: function () {
            if (!isTrue(this.name)){showToast("名称不能为空");return}
            if (!isTrue(this.pics)){showToast("请选择图片");return}
            apiPost("/api/userCertificates", {
                name:Page.name,
                pics:Page.pics
            }, function (res ,code) {
                logJson(res)
                if (code == 200){
                    showToast(res.message)
                    getData()
                    Page.picsView = []
                    Page.pics = []
                    Page.name = ""
                }else {
                    showToast("系统繁忙")
                }
            });
        }
    })
    Page.init()
}

function getData() {
    apiGet("/api/my/userCertificates",{},function (res,code) {
        logJson(res.data,code);
        Page.log = res.data
    })
}